/*
    This file is part of AvOS cli (AvOS Command Line Interface)

    Copyright (C) 2008 Avocent Corp.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
*/
/*    Author:Lynn Lin   <lynn.lin@avocent.com>                */

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<readline/readline.h>
#include<readline/history.h>

#include"version.h"

int flag = 0;
void display_usage() {
                fprintf(stderr, "Options:\n");
                fprintf(stderr, "-h\n\tHelp (this message)\n");
                fprintf(stderr, "-V(v)\n\tShow CLI version\n");
}

char *
buildPrompt() {
  if (0  == flag ) {
  	version();
	flag = 1;
     }
  return  ">";
}

int main(int argc,char *argv[])
{
	const char *options = "vVh";
	char * cmdLine = NULL;
	int c;
	int curl = 0;
	while ((c = getopt(argc, argv, options)) != -1) {
		switch (c) {
		case 'v':
		case 'V':
			++curl;
			// process the case like this :
			// cli -va 
			if (strlen(argv[curl]) > 2 ) {
				printf("unrecognized arguments\n");
				return -1;
			}
			version();
			return 0;
		case 'h':
			++curl;
			if (strlen(argv[curl]) > 2 ) {
				printf("unrecognized arguments\n");
				return -1;
			}
			version();
			display_usage();
			return EXIT_SUCCESS;
			
		default:
			return 'c' == '?' ? -1:EXIT_SUCCESS;
		}
			
	}

	if (argc > 1 ) {
		printf(" unrecognized arguments\n");
		return -1;
	}
	
	while(1){
	//insert your code to print prompt here.

    		cmdLine = readline(buildPrompt());
    		if (cmdLine == NULL) {
     			fprintf(stderr, "Unable to read command\n");
      			continue;
      		}
	}
	return EXIT_SUCCESS;
		
}
